package com.weiyun.notebook

import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.weiyun.notebook.Database.AppDatabase
import com.weiyun.notebook.Entity.Note
import kotlinx.android.synthetic.main.activity_edit.*
import java.text.SimpleDateFormat
import java.util.*

class EditActivity : AppCompatActivity() {
    private lateinit var noteViewModel: NoteViewModel
    lateinit var note: Note
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit)
        initData()
    }
    private fun initData() {
        val intent = intent
        note = intent.getSerializableExtra("note") as Note
        et_content.setText(note.content)
        et_title.setText(note.title)
        noteViewModel= ViewModelProvider(this,
            ViewModelProvider.AndroidViewModelFactory.getInstance(application)
        ).get(NoteViewModel::class.java)
    }

    fun save(view: View?) {
        val title: String = et_title.text.toString()
        val content: String = et_content.text.toString()
        if (TextUtils.isEmpty(title)) {
            ToastUtil.toastShort(this, "标题不能为空！")
            return
        }
        note.title = title
        note.content = content
        noteViewModel.update(note)
        ToastUtil.toastShort(this, "修改成功")
        finish()
    }
}